<template>
	<div>
		<CustomDialog :title="state.dialog.title" v-model:visible="state.dialog.isShowDialog" width="900px" @close="onCancel" :dialogFooterBtn="true">
			<template #default>
				<el-form ref="goodsServiceDialogRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
					<el-form-item label="名称" prop="serviceName">
						<el-input v-model="state.ruleForm.serviceName" placeholder="请输入标题" clearable></el-input>
					</el-form-item>
					<el-form-item label="服务标识" prop="serviceLogo">
						<div class="choice-image" @click="choiceFileImage">
							<el-image v-if="state.ruleForm.serviceLogo != ''" :src="STATIC_URL + state.ruleForm.serviceLogo" fit="contain" />
							<el-icon v-else>
								<ele-Plus />
							</el-icon>
						</div>
						<!-- <el-input v-model="state.ruleForm.serviceLogo" placeholder="请输入内容" :rows="4" type="textarea" clearable></el-input> -->
					</el-form-item>
					<el-form-item label="服务说明" prop="serviceExp">
						<el-input v-model="state.ruleForm.serviceExp" placeholder="请输入服务说明" clearable type="textarea"></el-input>
					</el-form-item>
				</el-form>
			</template>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(goodsServiceDialogRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</CustomDialog>

		<FileChoice @choice-file="choiceFileCallBack" ref="FileChoiceRef"></FileChoice>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref, defineAsyncComponent } from 'vue';
import { type FormInstance, type FormRules } from 'element-plus';
import { goodsServiceApi } from '/@/api/goods/service/index';
const FileChoice = defineAsyncComponent(() => import('/@/views/file/choice/index.vue'));
const FileChoiceRef = ref();
const emit = defineEmits(['refresh']);
import messageUtils from '/@/utils/messageUtils';

import { STATIC_URL } from '/@/utils/config';
const goodsServiceDialogRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		serviceName: '',
		serviceExp: '',
		serviceLogo: '',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const rules = reactive<FormRules>({
	activityName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
	activityTime: [{ required: true, message: '请选择活动时间 ', trigger: 'blur' }],
});

const choiceFileCallBack = (cfi: ChoiceFileInfo[]) => {
	console.log('cfi: ', cfi);

	state.ruleForm.serviceLogo = cfi[0].visitRelativePath;
};
const choiceFileImage = () => {
	FileChoiceRef.value.openFileChoiceDialog();
};
const onCancel = () => {
	closeDialog();
};

const closeDialog = () => {
	console.log('关闭窗口');
	state.dialog.isShowDialog = false;
	goodsServiceDialogRef.value.resetFields();
};

const openDialog = async (type: string, id: string) => {
	state.dialog.isShowDialog = true;
	if (type === 'edit') {
		state.dialog.title = '修改问题';
		state.dialog.type = 'edit';
		state.dialog.submitTxt = '更新';
		const { data } = await goodsServiceApi().view(id);
		state.ruleForm = data;
	} else {
		state.dialog.title = '创建问题';
		state.dialog.type = 'add';
		state.dialog.submitTxt = '新增';
	}
	state.dialog.isShowDialog = true;
};

const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate(async (valid) => {
		if (valid) {
			try {
				if ('edit' === state.dialog.type) {
					await goodsServiceApi().update(state.ruleForm);
				} else {
					await goodsServiceApi().create(state.ruleForm);
				}
				messageUtils().success('超过成功');
				closeDialog();
				emit('refresh');
			} catch (e) {
				messageUtils().error('操作失败,请重试');
			}
		}
	});
};
defineExpose({ openDialog });
</script>

<style scoped lang="scss"></style>
